package com.example.skindemo.ui.settings;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.GridLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.skindemo.databinding.FragmentSettingBinding;
import com.example.skindemo.loader.SkinApiManager;
import com.example.skindemo.ui.bean.SkinThemeBean;

import java.util.ArrayList;
import java.util.List;

import skin.support.SkinCompatManager;

public class SettingFragment extends Fragment {

    private SettingViewModel settingViewModel;
    FragmentSettingBinding skinBinding;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        settingViewModel =
                ViewModelProviders.of(this).get(SettingViewModel.class);
        skinBinding = FragmentSettingBinding.inflate(inflater, container, false);
        settingViewModel.getSkinThemeBeanMutableLiveData().observe(getViewLifecycleOwner()
                , new Observer<List<SkinThemeBean>>() {
                    @Override
                    public void onChanged(List<SkinThemeBean> skinThemeBeans) {
                        skinThemeBeanList.addAll(skinThemeBeans);
                    }
                });
        return skinBinding.getRoot();
    }

    List<SkinThemeBean> skinThemeBeanList = new ArrayList<>();


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


    }

    @Override
    public void onResume() {
        super.onResume();
        for (int i = 0; i < skinThemeBeanList.size(); i++) {
            SkinThemeBean skinThemeBean = skinThemeBeanList.get(i);
            if (skinThemeBean.isCheckTheme()) {
                skinBinding.skinThemeColorImg.setBackgroundResource(skinThemeBean.getThemeType());
                skinBinding.skinThemeColorTextTv.setText(skinThemeBean.getThemeName());
                break;
            }
        }
        skinThemeAdapter themeAdapter = new skinThemeAdapter(skinThemeBeanList);
        skinBinding.skinThemeRecyleview.setLayoutManager(new GridLayoutManager(getActivity(),
                2, GridLayoutManager.HORIZONTAL, false));
        skinBinding.skinThemeRecyleview.setAdapter(themeAdapter);

        themeAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter,
                                    @NonNull View view, int position) {
                if (skinThemeBeanList.get(position).isCheckTheme()) {
                    Log.i("测试  ", "已经是该皮肤了" +
                            skinThemeBeanList.get(position).toString());
                    return;
                }
                for (SkinThemeBean s : skinThemeBeanList) {
                    s.setCheckTheme(false);
                }
                skinThemeBeanList.get(position).setCheckTheme(true);
                adapter.notifyDataSetChanged();

                if (skinThemeBeanList.get(position).getThemeNameColor().equals("red")) {
                    setTheme(position);
                    return;
                }
                if (skinThemeBeanList.get(position).getThemeNameColor().equals("green")) {
                    setTheme(position);
                    return;
                }

                if (skinThemeBeanList.get(position).getThemeNameColor().equals("blue")) {
                    setTheme(position);
                    return;
                }
                SkinApiManager.loadSkinFrom(skinThemeBeanList.get(position).getThemeNameColor(),
                        new SkinApiManager.SkinLoaderListener() {
                            @Override
                            public void onStart() {

                            }

                            @Override
                            public void onSuccess() {

                            }

                            @Override
                            public void onFailed(String errMsg) {

                            }
                        }, SkinCompatManager.SKIN_LOADER_STRATEGY_BUILD_IN);
                skinBinding.skinThemeColorTextTv.setText(
                        skinThemeBeanList.get(position).getThemeName());
                skinBinding.skinThemeColorImg.setBackgroundResource(
                        skinThemeBeanList.get(position).getThemeType());
            }
        });
    }

    public void setTheme(int position) {
        SkinApiManager.loadSkinFromSdCard("theme_skins_" + skinThemeBeanList.get(position)
                .getThemeNameColor() + ".skin", new SkinApiManager.SkinLoaderListener() {
            @Override
            public void onStart() {
                Log.i("测试  ", "onStart");
            }

            @Override
            public void onSuccess() {
                Log.i("测试  ", "onSuccess");
            }

            @Override
            public void onFailed(String errMsg) {
                Log.i("测试  ", "onFailed" + errMsg);

            }
        });
        skinBinding.skinThemeColorTextTv.setText(skinThemeBeanList
                .get(position).getThemeName());
        skinBinding.skinThemeColorImg.setBackgroundResource(
                skinThemeBeanList.get(position).getThemeType());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        skinBinding = null;
    }

}